package com.oracle.calllog.producer;

import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.text.DecimalFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class CallLogsSimpleProducer {
    //用于存放随机通话记录的电话号码
    private List<String> phoneList = new ArrayList<>();
    //用于存放电话号码与人名的对应关系
    private Map<String, String> phoneNameMap = new HashMap<>();

    /**
     * 初始化电话号码&电话号码主人名称数据的方法
     */
    public void initPhone() {
        phoneList.add("17078388295");
        phoneList.add("13980337439");
        phoneList.add("14575535933");
        phoneList.add("19902496992");
        phoneList.add("18549641558");
        phoneList.add("17005930322");
        phoneList.add("18468618874");
        phoneList.add("18576581848");
        phoneList.add("15978226424");
        phoneList.add("15542823911");
        phoneList.add("17526304161");
        phoneList.add("15422018558");
        phoneList.add("17269452013");
        phoneList.add("17764278604");
        phoneList.add("15711910344");
        phoneList.add("15714728273");
        phoneList.add("16061028454");
        phoneList.add("16264433631");
        phoneList.add("17601615878");
        phoneList.add("15897468949");

        phoneNameMap.put("17078388295", "李雁");
        phoneNameMap.put("13980337439", "卫艺");
        phoneNameMap.put("14575535933", "仰莉");
        phoneNameMap.put("19902496992", "陶欣悦");
        phoneNameMap.put("18549641558", "施梅梅");
        phoneNameMap.put("17005930322", "金虹霖");
        phoneNameMap.put("18468618874", "魏明艳");
        phoneNameMap.put("18576581848", "华贞");
        phoneNameMap.put("15978226424", "华啟倩");
        phoneNameMap.put("15542823911", "仲采绿");
        phoneNameMap.put("17526304161", "卫丹");
        phoneNameMap.put("15422018558", "戚丽红");
        phoneNameMap.put("17269452013", "何翠柔");
        phoneNameMap.put("17764278604", "钱溶艳");
        phoneNameMap.put("15711910344", "钱琳");
        phoneNameMap.put("15714728273", "缪静欣");
        phoneNameMap.put("16061028454", "焦秋菊");
        phoneNameMap.put("16264433631", "吕访琴");
        phoneNameMap.put("17601615878", "沈丹");
        phoneNameMap.put("15897468949", "褚美丽");
    }

    /**
     * 生产随机通话记录数据<br>
     * 格式:18888888888,张飞,16666666666,关羽,2020-01-09 08:09:10,0090<br>
     * @param strStartDate 字符串格式起始日期,格式为:yyyy-MM-dd
     * @param strEndDate 字符串格式起始日期,格式为:yyyy-MM-dd
     */
    public String product(String strStartDate, String strEndDate) {
    	//主叫电话号码
        String callingParty = null;
        //被叫电话号码
        String calledParty = null;

        //主叫人
        String callingPartyName = null;
        //被叫人
        String calledPartyName = null;

        //生成随机主叫电话号码和主叫人
        int phoneNoIndex = (int) (Math.random() * phoneList.size());
        callingParty = phoneList.get(phoneNoIndex);
        callingPartyName = phoneNameMap.get(callingParty);
        while (true) {
        	//生成随机被叫电话号码和被叫人,规则:主叫人&被叫人不能相同
        	phoneNoIndex = (int) (Math.random() * phoneList.size());
            calledParty = phoneList.get(phoneNoIndex);
            calledPartyName = phoneNameMap.get(calledParty);
            if (!callingParty.equals(calledParty)) 
            	break;
        }
        //生成随机通话时间
        String buildTime = randomBuildTime(strStartDate, strEndDate);
        //生成随机通话时长,单位为秒,范围:[0,7200),以0000格式保存
        DecimalFormat df = new DecimalFormat("0000");
        String duration = df.format(120 * (int)(60 * Math.random()));
        
        StringBuilder builder = new StringBuilder();
        //格式:18888888888,张飞,16666666666,关羽,2017-01-09 08:09:10,0090
        builder.append(callingParty + ",")
		        .append(callingPartyName + ",")
	        	.append(calledParty + ",")
	        	.append(calledPartyName + ",")
	        	.append(buildTime + ",")
	        	.append(duration);
        return builder.toString();
    }

    /**
     * 根据传入的时间区间，在此范围内随机通话建立的时间<br>
     * startTimeTS + (endTimeTs - startTimeTs) * Math.random();<br>
     * @param strStartDate 字符串格式起始日期,格式为:yyyy-MM-dd
     * @param strEndDate 字符串格式起始日期,格式为:yyyy-MM-dd
     */
    public String randomBuildTime(String strStartDate, String strEndDate) {
        try {
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
            Date startDate = sdf.parse(strStartDate);
            Date endDate = sdf.parse(strEndDate);

            if (endDate.getTime() <= startDate.getTime()) 
            	return null;

			long randomTS = startDate.getTime() + (long) ((endDate.getTime() - startDate.getTime()) * Math.random());
            Date resultDate = new Date(randomTS);
            sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            String resultTimeString = sdf.format(resultDate);
            return resultTimeString;
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 将数据写入到文件中
     * @param strLogFilePath 将生成数据写入到的文件位置
     * @param strStartDate 字符串格式起始日期,格式为:yyyy-MM-dd
     * @param strEndDate 字符串格式起始日期,格式为:yyyy-MM-dd
     */
    public void writeLog(String strLogFilePath, String strStartDate, String strEndDate) {
    	OutputStreamWriter osw = null;
        try {
            osw = new OutputStreamWriter(new FileOutputStream(strLogFilePath), "UTF-8");
            while (true) {
            	//每秒产生一条通话记录，一秒就差不多了，虚拟机集群的压力大
                Thread.sleep(1000);
                String log = product(strStartDate, strEndDate);
                System.out.println(log);
                osw.write(log + "\n");
                //一定要手动flush才可以确保每条数据都写入到文件一次，但这样性能不算好
                osw.flush();
            }
        } catch (IOException e) {
            e.printStackTrace();
        } catch (InterruptedException e2) {
            e2.printStackTrace();
        } finally {
			if(osw!=null)
				try {
					osw.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
		}
    }

    
    public static void main(String[] args) throws InterruptedException {
        if (args == null || args.length <= 0) {
            System.out.println("no arguments");
            return;
        }
        //String logPath = "/tmp/calllog.csv";
        String logPath = args[0];
        String startDate = "2020-01-01";
        //默认终止时间点为当前时间的下一天
        long endTime = System.currentTimeMillis()+24*60*60*1000;
        String endDate = new SimpleDateFormat("yyyy-MM-dd").format(new Date(endTime));
        
        // 传递两个参数时，随机通话时间数据的起始时间点使用传递的参数，终止时间点使用默认值
        if(args.length==2)
        	startDate = args[1];
        //传递三个以上参数时，随机通话时间数据的起始和终止时间点都使用传递的参数
        if(args.length>=3) 
        	endDate = args[2];
        	
        CallLogsSimpleProducer productLog = new CallLogsSimpleProducer();
        productLog.initPhone();
        productLog.writeLog(logPath, startDate, endDate);
    }
}

